﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using AjaxControlToolkit;

public partial class TaskBoard : System.Web.UI.Page
{
    const int IDX_PBI = 1;
    const int IDX_DESCRIPTION = 5;
    const int IDX_PANELPOPUP = 7;
    const int IDX_PBIDETAILS = 3;
    const int IDX_DESCDETAILS = 9;
    const int IDX_PRIORITYDETAILS = 13;
    const int IDX_BTNDNPRIORITY = 15;
    const int IDX_BTNUPPRIORITY = 17;
    const int IDX_RADUSER = 21;
    const int IDX_DDLUSER = 23;
    const int IDX_RADSTATUS = 25;
    const int IDX_DDLSTATUS = 27;
    const int IDX_BTNMOVE = 29;
    const int IDX_BTNDEL = 31;
    const int IDX_HDNEMPID = 9;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        List<Employee> allEmployees = Employee.GetAllEmployees();
        //if (!IsPostBack)
        //{
            BindEmployeeData(allEmployees);
        //}
            CreateTaskIcons(allEmployees);
    }

    protected void Page_Load(object sender, EventArgs e)
    {        
    }

    void CreateTaskIcons(List<Employee> allEmployees)
    {
        if (gvTaskBoard.Rows.Count > 0)
        {
            int empId = 0;
            Controls_ucTaskIcon taskIconControl = null;
            List<Task> allTasks = Task.GetAllTasks();
            foreach (GridViewRow gridRow in gvTaskBoard.Rows)
            {
                //create icons status wise
                empId = Convert.ToInt32(gvTaskBoard.DataKeys[gridRow.RowIndex].Value);
                Employee currentEmployee = allEmployees.Where(emp => emp.EmpID == empId).FirstOrDefault();
                var query = from task in allTasks
                            where task.EmpID == empId
                            select task;
                for (Task.TaskStatus status = Task.TaskStatus.ToDo; status <= Task.TaskStatus.Done; status++)
                {
                    //add icons in grid status-wise
                    foreach (Task empTask in query.Where(task => task.Status == status).OrderByDescending(task => task.Priority))
                    {
                        
                        taskIconControl = (Controls_ucTaskIcon)LoadControl("~/Controls/ucTaskIcon.ascx");                        
                        Label labelPBI = (Label)taskIconControl.Controls[IDX_PBI];
                        labelPBI.Text = Convert.ToString(empTask.PBI);
                        Label labelDescription = (Label)taskIconControl.Controls[IDX_DESCRIPTION];
                        labelDescription.Text = empTask.Description.Substring(0, 5) + "...";
                        labelDescription.ToolTip = empTask.Description;
                        HiddenField hdnEmpID = (HiddenField)taskIconControl.Controls[IDX_HDNEMPID];
                        hdnEmpID.Value = Convert.ToString(empTask.EmpID);
                        Panel pnlPopUp = (Panel)taskIconControl.Controls[IDX_PANELPOPUP];
                        Label lblPBIDetails = (Label)pnlPopUp.Controls[IDX_PBIDETAILS];
                        lblPBIDetails.Text = Convert.ToString(empTask.PBI);
                        Label lblDescDetails = (Label)pnlPopUp.Controls[IDX_DESCDETAILS];
                        lblDescDetails.Text = empTask.Description;
                        Label lblPriorityDetails = (Label)pnlPopUp.Controls[IDX_PRIORITYDETAILS];
                        lblPriorityDetails.Text = Convert.ToString(empTask.Priority);
                        DropDownList ddlUser = (DropDownList)pnlPopUp.Controls[IDX_DDLUSER];
                        ddlUser.DataSource = allEmployees;
                        ddlUser.DataTextField = "FullName";
                        ddlUser.DataValueField = "EmpID";
                        ddlUser.DataBind();
                        ddlUser.SelectedIndex = ddlEmployee.Items.IndexOf(ddlEmployee.Items.FindByValue(Convert.ToString(empTask.EmpID)));
                        DropDownList ddlStatus = (DropDownList)pnlPopUp.Controls[IDX_DDLSTATUS];
                        Dictionary<Task.TaskStatus, string> statusDict = Task.GetTaskStatusListToDisplay();
                        ddlStatus.DataSource = statusDict;
                        ddlStatus.DataTextField = "Value";
                        ddlStatus.DataValueField = "Key";
                        ddlStatus.DataBind();
                        ddlStatus.SelectedIndex = ddlStatus.Items.IndexOf(ddlStatus.Items.FindByValue(Convert.ToString(empTask.Status)));
                        RadioButton radUser = (RadioButton)pnlPopUp.Controls[IDX_RADUSER];
                        radUser.Checked = true;
                        RadioButton radStatus = (RadioButton)pnlPopUp.Controls[IDX_RADSTATUS];
                        ddlStatus.Enabled = false;
                         
                        gridRow.Cells[Convert.ToInt32(status) + 1].Controls.Add(taskIconControl);
                    }
                }
            }
        }
    }

    

    /// <summary>
    /// Binds employee personal data to dropdown and gridview
    /// </summary>
    /// <param name="allEmployees"></param>
    void BindEmployeeData(List<Employee> allEmployees)
    {        
        DataTable dtEmployees = GetTaskBoardTableStructure();
        
        DataRow empRow = null;
        foreach (Employee emp in allEmployees)
        {
            //populate datatable
            empRow = dtEmployees.NewRow();
            empRow["EmpID"] = emp.EmpID;
            empRow["FullName"] = emp.FullName;
            dtEmployees.Rows.Add(empRow);

            //populate employee dropdown
            ddlEmployee.Items.Add(new ListItem(emp.FullName, Convert.ToString(emp.EmpID)));
        }
        
        //bind data to gridview
        gvTaskBoard.DataSource = dtEmployees;
        gvTaskBoard.DataBind();
    }

    DataTable GetTaskBoardTableStructure()
    {
        DataTable dtEmployees = new DataTable();
        dtEmployees.Columns.Add("EmpID");
        dtEmployees.Columns.Add("FullName");
        return dtEmployees;
    }

    protected void btnAddNewTask_Click(object sender, EventArgs e)
    {
        int pbi = Convert.ToInt32(tbxPBI.Text);
        string description = tbxDescription.Text;
        int empId = Convert.ToInt32(ddlEmployee.SelectedItem.Value);
        Task empTask = new Task(pbi, description, empId);
        bool addSuccess = empTask.AddNewTask();
        if (addSuccess)
        {
            ShowMessage("New task added successfully");
        }
        else
        {
            ShowMessage("Problem while adding new task");
        }
    }

    private void ShowMessage(string message)
    {
        string alertScript = @"<script> alert('" + message + @"');</script>";
        ClientScript.RegisterClientScriptBlock(GetType(), "msgBox", alertScript);
    }
}